import requests
import json
import aiofiles
import aiohttp
import asyncio
import os

dic = {
    "你好": "hello",
    "嗨": "hi",
    "英语": "English"
}
h = [1, 2, 3, 4]
l = list(dic.keys())
list = list(dic.values())


def test01():
    data = {
        "book_id": "4355370985",
        "cid": "4355370985|1566855962",
        "need_bookinfo": 1
    }
    data = json.dumps(data)
    url = f'https://dushu.baidu.com/api/pc/getChapterContent?data={data}'

    resp = requests.get(url)
    dic = resp.json()
    result = dic['data']['novel']['content']
    title = dic['data']['novel']['chapter_title']
    with open(title + ".txt", mode="w", encoding="utf-8") as f:
        f.write(result)

    print(result)


def aiodownload(title, cid, book_id):
    s = {  # json
        "book_id": book_id,
        "cid": f"{book_id}|{cid}",
        "need_bookinfo": 1
    }
    data = json.dumps(s)  # json转换成字符串
    url = f'https://dushu.baidu.com/api/pc/getChapterContent?data={data}'
    resp = requests.get(url)
    dic = resp.json()
    content = dic['data']['novel']['content']
    print(content)


def download_pic():
    # https://cdn2.zzzmh.cn/wallpaper/origin/b3eb9d03e5704f619f3fe55d809568ce.jpg?response-content-disposition=attachment&auth_key=1678809600-dd5af35dea6482ec398b8d3ef3e1b19d-0-dbe21a8276c0f747b087f4dae529af54
    url = "https://api.zzzmh.cn/bz/v3/download"
    data = {
        "id": "b3eb9d03e5704f619f3fe55d809568ce"
    }
    resp = requests.post(url, data)
    result = resp.content
    with open("图片.jpg", mode="wb")as f:
        f.write(result)

    print("over")


def merge(name, num):  # path:存放ts文件的上级文件夹
    # win条件下将ts文件合并成一个视频
    # mac: cat 1.ts 2.ts 。。。> xxx.mp4
    # windows: copy /b 1.ts+2.ts+... xxx.mp4
    list = []
    for i in range(1, num + 1):
        tl = f"TS/《斗罗大陆》第01集 - 酷播线路/{i}.ts"
        list.append(tl)
    # for i in list:
    #     print(i)
    string = " ".join(list)
    print(string)
    os.system(f"copy /b {string} > movie.mp4")
    print("搞定！")


'''
# 判断文件是否存在
import os
os.path.exists(test_file.txt)
#True

os.path.exists(no_exist_file.txt)
#False

# 判断文件夹是否存在
import os
os.path.exists(test_dir)
#True

os.path.exists(no_exist_dir)
#False

'''

if __name__ == '__main__':
    # aiodownload("nihao", "1566855961", "4355370985")
    # name = "《斗罗大陆》第01集 - 酷播线路"
    # updata_mp4(name)
    # print(os.path.exists("m3u8/1.m3u8"))
    # url1 = 'https:\/\/s1.zoubuting.com\/20210731\/Ippn8ThS\/index.m3u8'.replace("\\", "")
    # print(url1.rsplit("/", 3)[0])

    # 测试ts合并!!!!!!!失败
    name = "《斗罗大陆》第01集 - 酷播线路"
    ts_num = 578
    merge(name, ts_num)
